package com.api.workflow.action;

import cn.hutool.json.JSONUtil;
import weaver.general.BaseBean;
import com.api.workflow.util.CrmUtil;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

//修改客诉单
public class ModifyCustomerComplaintSheetAction implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        try{

//        ==============
            // 表单名称
            String tableName = requestInfo.getRequestManager().getBillTableName();
            // 表单数据ID
            int bill_id = requestInfo.getRequestManager().getBillid();

            String sql = "select * from "+ tableName +" where id = "+ bill_id;
            new BaseBean().writeLog("ModifyCustomerComplaintSheetAction ------------------------> sql:"+ sql);
            RecordSet recordSet = new RecordSet();
            recordSet.execute(sql);
            // crm 对应 oa字段
            HashMap<String, String> mainTableInfoMap = new HashMap<String, String>();
            mainTableInfoMap.put("oa_status__c", "zt");  // 审批状态
//        mainTableInfoMap.put("approval_node__c", "nodename");  // 审批节点
//        mainTableInfoMap.put("approval_opinion__c", "");  // 审批意见
            mainTableInfoMap.put("content__c", "nrbc");  // 内部补充
            mainTableInfoMap.put("field_H7Obb__c", "hlx");  // 合理性
            mainTableInfoMap.put("field_5dVN0__c", "zrbm");  // 责任部门
            mainTableInfoMap.put("handler1__c", "clr");  // 处理人
            mainTableInfoMap.put("processing_result__c", "cljg");  // 处理结果
            mainTableInfoMap.put("involved_amount__c", "sjje");  // 涉及金额
            mainTableInfoMap.put("reason_analysis__c", "yyfx");  // 原因分析
            mainTableInfoMap.put("preventive_corrective_meas__c", "yfjzcs");  // 预防纠正措施
            mainTableInfoMap.put("remark__c", "bz");  // 备注
            mainTableInfoMap.put("impact_assessment__c", "yxxpg");  // 影响性评估
            mainTableInfoMap.put("complaint_type__c", "tslx");  // 投诉类型
            mainTableInfoMap.put("_id", "crmid");
            if (recordSet.next()){
                HashMap<String, String> param = new HashMap<String, String>();
                for (Map.Entry<String, String> stringStringEntry : mainTableInfoMap.entrySet()) {
                    String key = stringStringEntry.getKey();
                    String value = stringStringEntry.getValue();
                    String string = recordSet.getString(value);
                    new BaseBean().writeLog("ModifyCustomerComplaintSheetAction ------------------------> key："+ key +  " value："+ value + " string："+ string);
                    param.put(key, string);
                }
                String token = CrmUtil.getToken();

                String remark = requestInfo.getRequestManager().getRemark(); //流程意见
                param.put("approval_opinion__c", remark);
                String requestid = requestInfo.getRequestid(); // 请求ID
                RecordSet re = new RecordSet();
                re.executeQuery(" select nodename from workflow_nodebase where id = (select currentnodeid from workflow_requestbase where requestid = ?)",requestid);
                if (re.next()){
                    param.put("approval_node__c", re.getString("nodename"));
                }
                new BaseBean().writeLog("ModifyCustomerComplaintSheetAction ------------------------> param："+ JSONUtil.toJsonStr(param));
                boolean b = CrmUtil.updateKsOrder(token,param);
                if (b){
                    new BaseBean().writeLog("ModifyCustomerComplaintSheetAction ------------------------>修改客诉单状态成功");
                }else {
                    new BaseBean().writeLog("ModifyCustomerComplaintSheetAction ------------------------>修改客诉单状态失败");
                    requestInfo.getRequestManager().setMessageid("11111"+requestInfo.getRequestid()+"22222");
                    requestInfo.getRequestManager().setMessagecontent("修改客诉单失败("+ requestid +"):"+param.get("errorMsg"));
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
        }catch (Exception e){
            new BaseBean().writeLog("ModifyCustomerComplaintSheetAction ------------------------> e:"+e);
        }
        return null;
    }
}
